#include "SDL.h"
#include "SDL_audio.h"
#include "SDL_mixer.h"
#include "audio.h"
#include <iostream>
#include <cstdio>
#include <string>

//This setup is pretty basic - I want to change it later to make it more friendly with multiple sounds playing at once
//To do this, I plan on making one class for the Sound System, and one class for sound objects

	// For multiple sounds, you may wish to make the sound system declared as a subset of vector<sound>: class soundSystem: public vector<sound> would do it. It makes it really easy to manage things, take my word for it.

//Brilliant.  I'll get to that in the next revision.  Right now, I just want to see if this madness will work

//I have some code for dealing with vectors of objects from a practice project of mine; I could send it if you wanted.
//Yeah, sure.  I'll take a look at it later - I'm tired right now, and not thinking quite right.
//I dropped in the code, as well as the Linux install for XChat and some IRC details. Talking in comments gets old. Once I finish my next experiment in these things (sometime tonight) I will add that code as well.

//Also, this probably doesn't quite work right now --- so don't touch!
//I still need to get streaming sound working before I'll have a product I can demonstrate...
//Mix_Chunk *audio = NULL;
int channel;
short currentchannel;
short available;  //how many channels are we able to use

int sound::setaudiomode(int fr,char cha,short chu)
{
    if((fr != 44100) && (fr !=22050) && (fr != 11025)) //keep frequency limited to common computer audio frequencies
    {
        fprintf(stderr, "Bad audio frequency! Check code!");
        exit(1);
    }
    freq = fr;
    switch(cha)
    {
    case 1:
        //Mono
        chan = 1;
        break;
    case 2:
        //Stereo
        chan = 2;
        break;
    default:
        fprintf(stderr, "Bad channel parameter! Check code!");
        exit(1);
    }
    chunk = chu;
    if(Mix_OpenAudio(freq, AUDIO_S16SYS, chan, chunk) != 0)
    {
    	fprintf(stderr, "Cannot open audio - Check settings?: %s\n", Mix_GetError());
    	exit(1);
    }
    return(0);
}

int sound::loadaudio(char* filename)
{
    audiofile = filename;
    cchunk tempchunk;
    tempchunk.mchunk = Mix_LoadWAV(filename);
    tempchunk.playing = 0;
    tempchunk.ch = -1;
    push_back(tempchunk);  //The name here is misleading - Mix_LoadWAV can open any audio format supported by SDL
    if(at(size() - 1).mchunk == NULL)
    {
        fprintf(stderr, "Cannot load sound file - Does the file exist?: %s\n", Mix_GetError());
        pop_back();
        exit(1);
    }
    return (size() - 1);
}

int sound::playaudio(short soundvector)
{
    
    if(at(soundvector).playing == 1) return 0;                       
    channel = Mix_PlayChannel(-1, at(soundvector).mchunk,0);  //Third parameter is for loop
    if(channel == -1) 
    {
	    fprintf(stderr, "Cannot play sound file: %s\n", Mix_GetError());
        exit(1);
    }
    at(soundvector).playing = 1;
    at(soundvector).ch = channel; 
    return(0);
}


int sound::soundloop(short positioninvector) 
{
    if(Mix_Playing(at(positioninvector).ch) == 0)
    {
        at(positioninvector).playing = 0;
        return 0;
    }else return(1);
}
   
void sound::cleanupsound() //After this is called, you can no longer use sdl_mixer functions
{
    Mix_CloseAudio();   
}
